package com.qhit.controller;

import com.qhit.pojo.Goods;
import com.qhit.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: lyb
 * @description: ll
 * @author: DYF
 * @create: 2020-10-22 09:33
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequiresPermissions("goods:attt")
    @RequiresRoles("user")
    @RequestMapping("/list")
    public String list(Model m ,String keywords,
                       @RequestParam(defaultValue = "1",required = false) Integer page,
                       @RequestParam(defaultValue = "2",required = false) Integer pageSize){

        Map map=goodsService.getListByPageWithSize(page,pageSize,keywords);
        m.addAttribute("goodsList",map.get("goodsList"));
        m.addAttribute("myParams",map.get("hashMap"));
        return "goods/list";
    }


    @RequestMapping("/toAdd")
    public String toAdd(){
        return "goods/add";
    }

    @RequestMapping(value = "/add",method= RequestMethod.POST)
    public String add(Goods goods,Model m){
        int result=goodsService.add(goods);
        if (result>0){
            m.addAttribute("msg","添加成功");
        }else {
            m.addAttribute("msg","添加失败");
        }
        return "goods/msg";
    }

    @RequestMapping("/toEdit")
    public String toEdit(String id,Model m){
        Goods g= goodsService.getGoodsById(id);
        m.addAttribute("goods",g);

        return "goods/edit";
    }

    @RequestMapping(value = "/edit",method= RequestMethod.POST)
    public String edit(Goods goods,Model m){
        int result=goodsService.edit(goods);
        if (result>0){
            m.addAttribute("msg","更新成功");
        }else {
            m.addAttribute("msg","更新失败");
        }
        return "goods/msg";
    }

    @RequestMapping(value = "/del",method= RequestMethod.GET)
    public String del(String id,Model m){
        int result=goodsService.del(id);
        if (result>0){
            m.addAttribute("msg","删除成功");
        }else {
            m.addAttribute("msg","删除失败");
        }
        return "goods/msg";
    }

}
